package com.mew.chess.core.game.lh.game.task;


import com.mew.chess.core.common.handler.ChessPushMessageUtil;
import com.mew.chess.core.game.lh.game.LhGameManager;
import com.mew.chess.core.game.lh.game.LhRoomManager;
import com.mew.chess.core.game.lh.game.common.LhConstantValue;
import com.mew.chess.core.game.lh.game.common.LhLinkType;
import com.mew.chess.core.game.lh.game.message.LhPushBetInfoMessage;
import com.mew.chess.core.game.lh.game.message.LhPushCMDMapType;
import com.mew.chess.core.game.lh.game.message.LhPushDealCardMessage;
import com.mew.common.dc.redis.GameRoomRedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class LhDealCardLhPaymentTask {
    @Autowired
    private LhPaymentTask paymentTask;

    @Autowired
    private GameRoomRedisService gameRoomRedisService;


    public void dealCard(int roomId){
        LhRoomManager roomManager = LhGameManager.getInstance().getController(roomId);
        LhPushBetInfoMessage betInfoMessage = roomManager.getBetInfoMessage();
        betInfoMessage.clearInfoList();
        dealCardLinkType(roomManager);

        LhPushDealCardMessage cardVO = doDealCard(roomManager);
        log.info("Lh dealCardINFO：{} ", cardVO.toString());

        sendPaymentMsg(roomManager,cardVO);
    }

    private void dealCardLinkType(LhRoomManager roomManager){
        roomManager.setRuleTime(LhConstantValue.DEAL_TIME);
        roomManager.setLinkType(LhLinkType.DealCard.code());
        roomManager.setStartTime(System.currentTimeMillis());
    }

    private LhPushDealCardMessage doDealCard(LhRoomManager roomManager){
        boolean capacityControl = gameRoomRedisService.isCapacityControl(roomManager.getGameId(), roomManager.getRoomId());

        //控制 0不控制 ，1 龙 ，2 虎
        int c = 0;

        if (capacityControl){
            List<Long> betNumbers = roomManager.getBetInfoMessage().getBetNumbers();
            c = betNumbers.get(1)>betNumbers.get(2)?2:1;

        }

        roomManager.getDealCardMessage().getLhGame().initDice(c);

        return LhPushDealCardMessage.builder()
                .remainTime(LhConstantValue.DEAL_TIME)
                .betInfoMessage(roomManager.getBetInfoMessage())
                .dealCardMessage(roomManager.getDealCardMessage())
                .build();
    }

    private void sendPaymentMsg(LhRoomManager roomManager,LhPushDealCardMessage cardVO){
        roomManager.getOnlineUser().forEach((k, session) -> {

            if (null != session){
                ChessPushMessageUtil.pushMessage(session, LhPushCMDMapType.DealCardMessage.code(),cardVO);

            }
        });
    }

}
